package leetcode.other;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 李栋
 * @version 1.0.0
 * @className Test0106.java
 * @createTime 2021年07月25日 20:58:00
 */
public class Test0106 {

    private static final Logger logger = LoggerFactory.getLogger(Test0106.class);

    @Test
    public void test() {
        logger.info(compressString("aabcccccaaa"));
    }

    public String compressString(String S) {
        char[] chars = S.toCharArray();
        int flag = 0;
        StringBuilder stringBuilder = new StringBuilder();
        String temp = "";
        for (char aChar : chars) {
            for (int i = 0; i < chars.length; i++) {
                if (aChar == chars[i] && !String.valueOf(aChar).equals(temp)) {
                    flag++;
                } else {
                    if (i == 0) {
                        temp = String.valueOf(chars[0]);
                    } else {
                        temp = String.valueOf(chars[i - 1]);
                    }
                    stringBuilder.append(aChar).append(flag);
                    flag = 0;
                    break;
                }
            }

        }
        return stringBuilder.toString();
    }
}
